package com.zjb.demo.framework.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @author 庄佳彬
 * @since 2023/12/28 22:57
 */
public class DataScopeConverter implements Converter<String> {
    // "1=所有数据权限,2=自定义数据权限,3=本部门数据权限,4=本部门及以下数据权限,5=仅本人数据权限"
    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        var value = cellData.getStringValue();
        return switch (value) {
            case "所有数据权限" -> "1";
            case "自定义数据权限" -> "2";
            case "本部门数据权限" -> "3";
            case "本部门及以下数据权限" -> "4";
            case "仅本人数据权限" -> "5";
            default -> value;
        };
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        var data = switch (value) {
            case "1" -> "所有数据权限";
            case "2" -> "自定义数据权限";
            case "3" -> "本部门数据权限";
            case "4" -> "本部门及以下数据权限";
            case "5" -> "仅本人数据权限";
            default -> value;
        };
        return new WriteCellData<>(data);
    }
}
